<?php

/*
 *	发送信息到 LCD 显示器显示环境信息
 */

/* 设置时区 */
date_default_timezone_set('PRC');

$c = include(__DIR__."/../config.php");

/**
 * 下发控制指令
 *
 * AF_INET	IPv4 网络协议。TCP 和 UDP 都可使用此协议。
 * SOCK_STREAM	提供一个顺序化的、可靠的、全双工的、基于连接的字节流。
 *                  支持数据传送流量控制机制。TCP 协议即基于这种流式套接字。
 * TCP 能够保障所有的数据包是按照其发送顺序而接收的。
 *      如果任意数据包在通讯时丢失，TCP 将自动重发数据包直到目标主机应答已接收。
 *      因为可靠性和性能的原因，TCP 在数据传输层使用 8bit 字节边界。
 *      因此，TCP 应用程序必须允许传送部分报文的可能。
 *
 * @param $data
 * @param $ip
 * @param $port
 * @param bool $type
 * @param int $timeout
 * @return bool
 */
class Basic {
	
	private $config;
	
	private $redis;

	private $socket;
	
	public function __construct($config, $redis) {
		$this->config = $config;
		$this->redis = $redis;
		$this->socket = 0;
	}
	
	/**
	 * 连接 redis
	 */
	private function linkRedis($c) {
		$redis = new Redis();
		$redis->connect($c['REDIS_HOST'], $c['REDIS_PORT']);
		$redis->select($c['REDIS_SELESE']);
		return $redis;
	}

	private function sendData($data, $ip, $port, $type = false, $timeout = 5) {
		if (($this->socket == 0) && ($this->socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP))) {
			$attempts = 0;
			/* 检查链接 */
			while (!($connected = @socket_connect($this->socket, $ip, $port)) && $attempts++ < $timeout) {
				$error = socket_last_error();
				if ($error != SOCKET_EINPROGRESS && $error != SOCKET_EALREADY) {
					return false;
				}
				sleep(1);
			}
			if (!$connected) {
				return false;
			}
		} else {
			/* 接收消息 */
			if($type && ($response = socket_read($this->socket, 1024))) {}
			/* 发送信息 */
			socket_write($this->socket, $data, strlen($data));
		}
		return true;
	}

    /**
     * 显示日期
     */
    public function showTimeControl() {

        $title = "SHOW/:SHOW001";
        $time = date('H:i');
        $date = date('Y/m/d');
        
        self::sendData("$title/:LINE_1", $this->config['SWOOLE_SERVER'], $this->config['SWOOLE_PORT']); sleep(2);
        self::sendData("$title/:Time.   $time   ", $this->config['SWOOLE_SERVER'], $this->config['SWOOLE_PORT']); sleep(2);
        self::sendData("$title/:LINE_2", $this->config['SWOOLE_SERVER'], $this->config['SWOOLE_PORT']); sleep(2);
        self::sendData("$title/:Date. $date", $this->config['SWOOLE_SERVER'], $this->config['SWOOLE_PORT']);

        return 1;
    }
	
	/**
     * 显示湿度
     */
	public function showHumidity() {
		$title = "SHOW/:SHOW001";
		$hum = $this->redis->get('HUMIDITY%HOME');
		self::sendData("$title/:LINE_1", $this->config['SWOOLE_SERVER'], $this->config['SWOOLE_PORT']); sleep(2);
		self::sendData("$title/:Hu:$hum"."% ".date('Hi'), $this->config['SWOOLE_SERVER'], $this->config['SWOOLE_PORT']); sleep(2);
	}
	
	/**
     * 显示温度
     */
	public function showTemp() {
		$title = "SHOW/:SHOW001";
		$temp = $this->redis->get('TEMP%HOME');
		$ir = (int)$this->redis->get('IRTEMP%IRTEMP001');
		self::sendData("$title/:LINE_2", $this->config['SWOOLE_SERVER'], $this->config['SWOOLE_PORT']); sleep(2);
		self::sendData("$title/:Te:$temp"."C Ir:$ir"."C", $this->config['SWOOLE_SERVER'], $this->config['SWOOLE_PORT']); sleep(2);
	}

}

$redis = new Redis();
$redis->pconnect($c['REDIS_HOST'], $c['REDIS_PORT']);
$redis->select($c['REDIS_SELESE']);

$home = new Basic($c, $redis);

while(1) {
	$home->showHumidity();
	$home->showTemp();
	sleep(10);
}